package com.zijie.shopbrandapi.api;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zijie.brand.model.Brand;
import com.zijie.brand.model.search.BrandSearchBean;
import com.zijie.brand.service.BrandService;

import com.zijie.core.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RefreshScope
@RestController
@RequestMapping("/api/v1/brand")
public class BrandApi {
    private BrandService brandService;

    @Autowired
    public void setBrandService(BrandService brandService) {
        this.brandService = brandService;
    }
    @GetMapping
    public ResponseEntity<JsonResult<?>> findAll(
            @RequestParam(defaultValue = "1")Integer pageNo,
            @RequestParam(defaultValue = "10")Integer pageSize
            , BrandSearchBean bsb) {
        Page<Brand> page = Page.of(pageNo, pageSize);
        page=brandService.findAll(page, bsb);
        return ResponseEntity.ok(JsonResult.success(page));
    }
//    添加
    @PostMapping("/add")
    public ResponseEntity<JsonResult<?>> add(@RequestBody Brand brand) {
        return ResponseEntity.ok(JsonResult.success(brandService.save(brand)));
    }
//    修改
    @PutMapping("/update")
    public ResponseEntity<JsonResult<?>> update(@RequestBody Brand brand) {
        return ResponseEntity.ok(JsonResult.success(brandService.update(brand)));
    }
//    删除多个
    @DeleteMapping("/delete")
    public ResponseEntity<JsonResult<?>> delete(@RequestParam("ids") List<Integer> ids) {
        return ResponseEntity.ok(JsonResult.success(brandService.deleteByIds(ids)));
    }
    //单个删除
    @DeleteMapping("/deleteById/{id}")
    public ResponseEntity<JsonResult<?>> deleteById(@PathVariable Integer id) {
        return ResponseEntity.ok(JsonResult.success(brandService.deleteById(id)));
    }
    @GetMapping("/findById/{id}")
    public ResponseEntity<JsonResult<?>> findById(@PathVariable Integer id) {
        Brand brand = brandService.findById(id);

        if (brand == null) {
            return ResponseEntity.ok(JsonResult.error("未找到该品牌"));
        }
        return ResponseEntity.ok(JsonResult.success(brand));
    }
}
